﻿<cfsilent>
	<cfscript>
		
		/* 处理当前排课学期数据 */	
		event.setArg("pageTitle", "教学运行基础数据 - 教学计划 - 教学任务下达");
	
		dictAdvice = getProperty("serviceFactory").getBean("senateDictionaryAdvice");

		rs_subject = queryNew("sbj_id");

		academicYear = event.getArg("AcademicYear");
		academicTerm = event.getArg("AcademicTerm");

		if ( len(academicYear) and len(academicTerm) ) {
			
			sql = "SELECT 
						t_institute.institute_short, 
						t_subject.sbj_id, 
						t_subject.sbj_name, 
						t_subject.term_start, 
						t_subject.level_code, 
						COUNT(DISTINCT t_schema.grade) countGrade, 
						COUNT(DISTINCT t_schema.cid) countCid 
					FROM 
						t_schema 
						INNER JOIN t_subject ON t_subject.sbj_id = t_schema.sbj_id 
						INNER JOIN t_institute ON t_institute.institute_id = t_subject.institute_id 
					WHERE 
						t_schema.academic_year = :academicYear 
						AND 
						t_schema.academic_term = :academicTerm 
					GROUP BY 
						t_institute.institute_short, 
						t_subject.sbj_id, 
						t_subject.sbj_name, 
						t_subject.term_start, 
						t_subject.level_code 
					ORDER BY 
						t_subject.sbj_id " ;
			queryObj = new query( datasource=application.dnsSlave );
			queryObj.addParam( name="academicYear", value=academicYear, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="academicTerm", value=academicTerm, cfsqltype="cf_sql_char" );
			rs_subject = queryObj.execute( sql=sql ).getResult();

		}

	</cfscript>
</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
			<div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('planSchema')#</cfoutput>"><i class="mrs img btnPublish"></i><span class="uiButtonText">教学计划</span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 course"></i>教学任务下达
				</h2>
			</div>
		</div>
	</div>
</div>

<div class="UIElement">
	<ul class="senateFirstPage">
		<cfif len(academicYear) and len(academicTerm)>
			<!--- 活动学期数据正确 --->
			<li>
				<div class="clearfix" id="subjectList">
					
					<div class="UIProgressbar">
						<span class="progressBar" id="pb1">0%</span>
						<p id="output">处理队列初始化</p>
					</div>
					
					<cfset row = 0 />
					<table class="UIEditable">
						<thead>
							<tr>
								<td width="18"></td>
								<td width="40">学院</td>
								<td>专业</td>
								<td width="90">授课对象</td>
								<td width="30">年级</td>
								<td width="50">课程门数</td>
								<td width="18"></td>
							</tr>
						</thead>
						<tbody>
                            <cfloop query="rs_subject">
								<cfset row ++ />
								<tr class="editRows" rowid="<cfoutput>#rs_subject.sbj_id#</cfoutput>">
									<td><span class="index"><cfoutput>#row#</cfoutput></span></td>
									<td><cfoutput>#rs_subject.institute_short#</cfoutput></td>
									<td><cfoutput>#rs_subject.sbj_name#</cfoutput></td>
									<td><cfoutput>#dictAdvice.getTermEnrol(rs_subject.term_start)#-#dictAdvice.getStudentLevel(rs_subject.level_code)#</cfoutput></td>
									<td align="center"><span class="numeric"><cfoutput>#rs_subject.countGrade#</cfoutput></span></td>
									<td align="center"><span class="numeric"><cfoutput>#rs_subject.countCid#</cfoutput></span></td>
									<td><span class="stat todo"></span></td>
								</tr>
							</cfloop>
						</tbody>
					</table>
					
					<cfset sbjList = "" />
                    <cfloop query="rs_subject">
						<cfset sbjList = listAppend(sbjList, "'" & rs_subject.sbj_id & "'") />
					</cfloop>
					
					<script language="javascript" type="text/javascript">
						//<![CDATA[
						<!--
						$(document).ready(function() {
						
							/* init Progress */
							$("#pb1").progressBar({
								boxImage		: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbar.gif',
								barImage		: {
													0:  '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_red.gif',
													30: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_orange.gif',
													70: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_green.gif'
													}
								});
								
							
							var subject = [<cfoutput>#sbjList#</cfoutput>];
							
							/* built each subject task */
							
							function progress(current) {
								
								$.post(
									"<cfoutput>#buildURL('apiRebuildSubjectPlan')#</cfoutput>",
									{
										'subjectID': subject[current],
										'academicYear': '<cfoutput>#academicYear#</cfoutput>',
										'academicTerm': '<cfoutput>#academicTerm#</cfoutput>',
										'seed': (new Date()).getTime()
									},
									function(data){

										response = $.parseJSON(data);
										
										/* fresh grid stats icons */
										var rowid = response.subject.SubjectID;
										var $row = $("tr.editRows[rowid='" + rowid + "']", ".UIEditable");										
										$("span.stat", $row).removeClass("todo").addClass("checked");
										
										/* fresh progress bar */
										var percentage = Math.floor(100 * parseInt(current + 1) / parseInt(subject.length));
										$("#pb1").progressBar(
																percentage,
																{
																	boxImage : '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbar.gif',
																	barImage : {
																		0:  '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_red.gif',
																		30: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_orange.gif',
																		70: '<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/widget/progressbar/images/progressbg_green.gif'
																},
																callback: function(running){
																	
																	if (running.running_value == running.value) {
																	
																		current++;
											
																		if ( current < subject.length) {
																			progress(current);
																		}
																		else {
																			$("#output").text("完成");
																		}
																	}
																}
																});
										
										/* fresh message */
										$("#output").text("正在处理专业教学计划 " + response.subject.SubjectName);
										
									});
								
							}
							
							progress(0);
							
						});
						// -->
						//]]>				
					</script>
					
				</div>
			</li>
			<cfelse>
			<!--- 要求设置一个数据处理学期 --->
			<li>
				<div class="clearfix" id="termSelector">
					
                   <cfset sql = "SELECT
										term_id, 
										academic_year, 
										academic_term 
									FROM 
										t_term 
									WHERE 
										SUBSTR(academic_year, 1, 4) > :yearStart 
										AND 
										SUBSTR(academic_year, 1, 4) < :yearEnd 
									ORDER BY 
										term_id " />
					<cfset queryObj = new query( datasource=application.dnsSlave ) />
					<cfset queryObj.addParam( name="yearStart", value=(year(now())-2), cfsqltype="cf_sql_char" ) />
					<cfset queryObj.addParam( name="yearEnd", value=(year(now())+1), cfsqltype="cf_sql_char" ) />
                    <cfset rs_term = queryObj.execute( sql=sql ).getResult() />
        
					<div class="noticeBlock">
						<p><span class="img icon16x16 message"></span>请指定当前教学计划的学期</p>
						<ol>
                            <cfloop query="rs_term">
								<li class="friendly"><span class="img icon10x10 execute"></span><a rowid="<cfoutput>#rs_term.term_id#</cfoutput>" href="javascript:void(0);"><cfoutput>#rs_term.academic_year#</cfoutput>-<cfoutput>#dictAdvice.getTermName(rs_term.academic_term)#</cfoutput></a></li>
							</cfloop>
						</ol>
					</div>
					
					<script language="javascript" type="text/javascript">
						//<![CDATA[
						<!--
						$(document).ready(function() {
							
							$("a[rowid]").click(function(){
							
								var rowid = $(this).attr("rowid");
							
								$.post(
										"<cfoutput>#buildURL('apiTermPropChange')#</cfoutput>", 
										{
											'tid' : rowid,
											'prop' : "ForCourse",
											'status' : true,
											'seed': (new Date()).getTime()
										}, 
										function(data) {
										
											result = $.parseJSON(data);
										
											if (result.ForCourse) {
												window.location.reload(true);
											}
										
										});
							});
							
						});
						// -->
						//]]>
					</script>
					
				</div>
			</li>
		</cfif>		
	</ul>
</div>